#include"Server.hpp"


#define HOST "127.0.0.1"
#define USER "root"
#define PASS "123456"
#define PORT 3306
#define DBNAME "gobang"

using namespace OnlineGoBang;

void test_db()
{
    UserTable ut(HOST, USER, PASS, DBNAME, PORT);
    Json::Value user;
    user["username"] = "小明";
    user["password"] = "1234567";
    ut.Insert(user);
    bool ret = ut.Login(user);
    if(ret)
    {
        std::cout<<"用户登录验证成功"<<std::endl;
    }
    Json::Value ans;
    ut.Win(5);
    LOG(DEBUG,"Win 执行完成\n");
    ut.Select_by_ID(5,ans);
    LOG(DEBUG,"Select_by_ID 执行完成\n");
    std::string str;
    Detail::JSON::Serialize(ans,str);
    std::cout<<str<<std::endl;

    Json::Value res;
    ut.Lose(5);
    ut.Select_by_Name("小明",res);
    Detail::JSON::Serialize(res,str);
    std::cout<<str<<std::endl;

}

void test_OnlineManager()
{
    uint64_t uid1=1,uid2=3;
    wsserver_t::connection_ptr con1,con2;
    OnlineManager om;
    om.EnterGameHall(uid1,con1);
    om.EnterGameRoom(uid2,con2);
    LOG(DEBUG,"用户进入成功\n");
    bool ret=om.InHall(uid1);
    if(ret)
    {
        std::cout<<uid1<<"在大厅中"<<std::endl;
    }
    else
    {
        std::cout<<uid1<<"不在大厅中"<<std::endl;
    }
    ret=om.InRoom(uid1);
    if(ret)
    {
        std::cout<<uid1<<"在房间中"<<std::endl;
    }
    else
    {
        std::cout<<uid1<<"不在房间中"<<std::endl;
    }

    ret=om.InHall(uid2);
    if(ret)
    {
        std::cout<<uid2<<"在大厅中"<<std::endl;
    }
    else
    {
        std::cout<<uid2<<"不在大厅中"<<std::endl;
    }
    ret=om.InRoom(uid2);
    if(ret)
    {
        std::cout<<uid2<<"在房间中"<<std::endl;
    }
    else
    {
        std::cout<<uid2<<"不在房间中"<<std::endl;
    }
    LOG(DEBUG,"用户查找成功\n");
    om.ExitGameHall(uid1);
    om.ExitGameRoom(uid2);
}
void test_Session()
{
    wsserver_t server;
    SessionManager sm(&server);
}
void test_server()
{
    // Server server(HOST, USER, PASS, DBNAME, PORT);//(ut,om,rm,wss,sm,mm);
    // server.Start(8888);

    UserTablePtr ut(new UserTable(HOST, USER, PASS, DBNAME, PORT)) ;
    OnlineManagerPtr om(new OnlineManager);
    RoomManagerPtr rm(new RoomManager(ut,om));
    MatchManagerPtr mm(new MatchManager(rm,ut,om));
    wsserver_ptr wss(new wsserver_t);
    SessionManagerPtr sm(new SessionManager(wss));
    Server server(ut,om,rm,wss,sm,mm);
    server.Start(8888);
}


int main()
{
    //test_db();
    
    test_server();
    //test_OnlineManager();
    //test_Session();
    return 0;
}